<?php

namespace app\controller\system;

use app\BaseController;
use app\model\StationAccount;
use app\model\StationBasic;
use app\model\StationBrand;
use app\model\StationOilCategory;
use app\model\StationOilNumber;
use app\model\StationOperation;
use app\model\StationPayment;
use app\model\StationPrice;
use app\model\StationTask;
use app\model\StationType;
use app\model\Tips;
use think\response\Json;

class Station extends BaseController
{
    public function lst(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\StationBasic::class)->pageData();
        });
    }

    public function detail(): Json
    {
        return $this->handle(function () {
//            return $this->app->make(StationBasic::class)->with(['payment','product'])->find($this->request->param('id'));
            $basic = $this->app->make(StationBasic::class)->find($this->request->param('id'));
            if(empty($basic)) throw new \Exception('数据不存在');
            $basic = $basic->toArray();
            $basic['active_tip'] = !empty($basic['active_tip'])?explode(",", $basic['active_tip']):[];
            $basic['service_time'] = explode("-", $basic['service_time']);
            $basic['station_tip'] = (int)$basic['station_tip'];
            if(empty($basic['city'])){
                $basic['city'] = $basic['country'];
                $basic['city_id'] = $basic['country_id'];
            }
            $payment = $this->app->make(StationPayment::class)->where('station_id', $this->request->param('id'))->find();
            $product = $this->app->make(StationPrice::class)->with(['supplier'])->where('station_id', $this->request->param('id'))->select();
            return compact('basic', 'payment', 'product');
        });
    }

    public function search()
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\StationBasic::class)->search();
        });
    }

    public function save(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\StationBasic::class)->saveData();
        });
    }

    public function stationCity(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\Stations::class)->stationCity();
        });
    }

    public function addTask(): Json
    {
        return $this->handle(function () {
            return $this->app->make(StationTask::class)->saveTask();
        });
    }

    public function task_lst(): Json
    {
        return $this->handle(function () {
            return $this->app->make(StationTask::class)->lst();
        });
    }

    public function account(): Json
    {
        return $this->handle(function () {
            return $this->app->make(StationAccount::class)->lst();
        });
    }

    public function account_save(): Json
    {
        return $this->handle(function () {
            return $this->app->make(StationAccount::class)->saveData();
        });
    }

    public function account_detail(): Json
    {
        return $this->handle(function () {
            return $this->app->make(StationAccount::class)->with(['station'])->where('id', $this->request->param('id'))->find();
        });
    }

    public function typeSave(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\StationType::class)->saveData();
        });
    }

    public function typeLst(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\StationType::class)->lst();
        });
    }

    public function typeDetail(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\StationType::class)->where('id', $this->request->param('id'))->find();
        });
    }

    public function brandSave(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\StationBrand::class)->saveData();
        });
    }

    public function brandLst(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\StationBrand::class)->lst();
        });
    }

    public function brandDetail(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\StationBrand::class)->find($this->request->param('id'));
        });
    }

    public function editCondition(): Json
    {
        return $this->handle(function () {
            //获取运营商
            $operation = $this->app->make(StationOperation::class)->where('state',1)->column("id,name");
            //供货商及服务区
            $merchant = $this->app->make(\app\model\Merchant::class)->with(['merSer'=>function ($query) {
                $query->where('state',1)->field('id,mid,name');
            }])->where('state',1)->select();
            //油站类型
            $stationType = $this->app->make(StationType::class)->where('state',1)->column("id,name");
            //油站品牌
            $stationBrand = $this->app->make(StationBrand::class)->where('state',1)->column("id,name");
            //返利档位
            $stationComs = ["无返利","A","B","C","D"];
            //获取站点标签
            $stationTip = $this->app->make(Tips::class)->where('state',1)->where('type',1)->column("id,name");
            //获取宣传标签
            $actTip = $this->app->make(Tips::class)->where('state',1)->where('type',2)->column("id,name");
            //获取油品
            $oil = $this->app->make(StationOilNumber::class)->where('state',1)->column("id,name");
            $oilType = $this->app->make(StationOilCategory::class)->where('state',1)->column("id,name");
            return compact("operation", "stationType", "stationBrand", "stationComs","merchant",'actTip','stationTip','oil','oilType');
        });
    }

    public function add_balance(): Json
    {
        return $this->handle(function (){
            return $this->app->make(StationBasic::class)->add_balance();
        });
    }
}
